package com.frame.work.frame.deal.device0fb4;


import com.alibaba.fastjson.JSONObject;
import com.frame.work.common.util.JsonToMqtt;
import com.frame.work.config.SpringUtil;
import com.frame.work.entity.TblSensirionLog;
import com.frame.work.frame.deal.base.BaseFrame;
import com.frame.work.mapper.TblGatewayMapper;
import com.frame.work.service.TblSensirionLogService;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static java.lang.Float.parseFloat;

/**
 * 温湿度传感器0FB40102
 */
@Data
public class WenShiDuChuanGanQi_0FB40102 extends BaseFrame {

    private Integer switchState;

    public WenShiDuChuanGanQi_0FB40102(String gatewayMac, String data) {
        //将基础数据进行处理
        super(gatewayMac, data);

        //处理此设备特有的数据
        this.setSwitchState(-100);

        //将actions信息放入json对象中
        this.setActions(getActionMap(data));
        //将温度和湿度进行日志保存
        saveTempAndHumidity(data, this.getGatewayMac(), this.getDeviceMac());

        JsonToMqtt.sendMQTT(gatewayMac, JSONObject.toJSONString(this));
    }

    /**
     * @Description:截取温度
     * @param: data
     * @return: java.lang.float
     * @author:wjming
     * @Date:2021/1/25 12:32
     */
    private static float getTemp(String data) {

        String temp = StringUtils.substring(data, data.length() - 10, data.length() - 8) + StringUtils.substring(data, data.length() - 12, data.length() - 10);
        float temp1 = parseFloat(temp.substring(0, 3) + '.' + temp.substring(3, 4));
        return temp1;
    }

    /**
     * @Description:截取湿度
     * @param: data
     * @return: float
     * @author:wjming
     * @Date:2021/1/25 12:34
     */
    private static float getHumidity(String data) {
        String humidity =  StringUtils.substring(data, data.length() - 4, data.length() - 2) +
                StringUtils.substring(data, data.length() - 6, data.length() - 4);
        float humidity1 = parseFloat(humidity.substring(0, 3) + '.' + humidity.substring(3, 4));
        return humidity1;
    }


    /**
     * @Description: 温度报警(预留字段)
     * @param: data
     * @return: java.lang.String
     * @author:wjming
     * @Date:2021/1/25 12:39
     */
    private static String getTempAlarm(String data) {
        return StringUtils.substring(data, data.length() - 8, data.length() - 6);
    }

    /**
     * @Description:湿度报警(预留字段)
     * @param: data
     * @return: java.lang.String
     * @author:wjming
     * @Date:2021/1/25 12:44
     */
    private static String getHumidityAlarm(String data) {
        return StringUtils.substring(data, data.length() - 2, data.length());
    }

    /**
     * @Description: 创建设备action对象信息，用于发送mqtt以及设备状态信息保存
     * @param: data
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     * @author:wjming
     * @Date:2021/1/25 12:47
     */
    private static Map<String, Object> getActionMap(String data) {
        Map<String, Object> actions = new HashMap();
        actions.put("temp", getTemp(data));
        actions.put("tempAlarm", getTempAlarm(data));
        actions.put("humidity", getHumidity(data));
        actions.put("humidityAlarm", getHumidityAlarm(data));
        return actions;
    }

    /**
     * @Description:保存设备日志
     * @param: data
     * @param: gatewayMac
     * @param: deviceMac
     * @return: void
     * @author:wjming
     * @Date:2021/1/25 13:34
     */
    public static void saveTempAndHumidity(String data, String gatewayMac, String deviceMac) {
        StringRedisTemplate redisTemplate = SpringUtil.getBean(StringRedisTemplate.class);
        if (redisTemplate.opsForValue().get("meteringLog_" + deviceMac) == null) {
            float temp = getTemp(data);
            float humidity = getHumidity(data);
            //进行数据库操作
            TblSensirionLogService sensirionLogService = SpringUtil.getBean(TblSensirionLogService.class);

            TblGatewayMapper gatewayMapper = SpringUtil.getBean(TblGatewayMapper.class);
            String userId = gatewayMapper.getUserIdByGatewayMac(gatewayMac);
            TblSensirionLog sensirionLog = TblSensirionLog.builder().gatewayMac(gatewayMac)
                    .deviceMac(deviceMac)
                    .humidity((double) humidity)
                    .temperature((double) temp)
                    .userId(userId).build();
            sensirionLogService.subTableSaving(sensirionLog);

            redisTemplate.opsForValue().set("meteringLog_" + deviceMac, temp + "_" + humidity, 10, TimeUnit.MINUTES);
        }
    }
}

